package cn.memorynine.consumer.service;

import cn.hutool.core.bean.BeanUtil;
import cn.memorynine.dao.entity.Activity;
import cn.memorynine.dao.entity.ActivityState;
import cn.memorynine.dao.entity.Consumer;
import cn.memorynine.dao.mapper.ActivityMapper;
import cn.memorynine.dao.mapper.ActivityStateMapper;
import cn.memorynine.dao.mapper.ConsumerMapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Service;
import cn.memorynine.common.model.ResponseWrapper;
import cn.memorynine.consumer.model.activity.manage.*;
import org.springframework.transaction.annotation.Transactional;

import java.beans.Transient;
import java.util.ArrayList;
import java.util.List;

/**
* 管理后台-活动管理服务
* @author 代码生成器
*/
@Slf4j
@Service
public class ActivityManageService {

    @Autowired
    private ActivityMapper activityMapper;

    @Autowired
    private ConsumerMapper consumerMapper;

    @Autowired
    private ActivityStateMapper activityStateMapper;

    /**
     * 1.获取所有活动列表
     * @return
     */
    public ResponseWrapper activityList(ActivityListParam param){
        ActivityListResult activityListResult = new ActivityListResult();
        List<Activity> activityList;
        Page<Activity> pageInfo = new Page<>(param.getPageNum(), param.getNum(),false);
        List<ActivityList> activityLists = new ArrayList<>();
        if (param.getStateId() == -1) {
            //获取活动总数
            activityListResult.setTotalNum(activityMapper.selectCount(
                    Wrappers.<Activity>lambdaQuery()
                            .like(Activity::getTitle, param.getSearchKey())
            ));
            Page<Activity> activityPage = activityMapper.selectPage(pageInfo,
                    Wrappers.<Activity>lambdaQuery()
                            .like(Activity::getTitle, param.getSearchKey())
                            .orderByAsc(Activity::getStateId)
            );
            activityList = activityPage.getRecords();

        } else {
            //获取活动总数
            activityListResult.setTotalNum(activityMapper.selectCount(
                    Wrappers.<Activity>lambdaQuery()
                            .like(Activity::getTitle, param.getSearchKey())
                            .eq(Activity::getStateId,param.getStateId())
            ));
            Page<Activity> activityPage = activityMapper.selectPage(pageInfo,
                    Wrappers.<Activity>lambdaQuery()
                            .like(Activity::getTitle, param.getSearchKey())
                            .eq(Activity::getStateId,param.getStateId())
                            .orderByAsc(Activity::getStateId)
            );
            activityList = activityPage.getRecords();
        }
        activityList.forEach(item->{
            ActivityList activityListItem = new ActivityList();
            BeanUtil.copyProperties(item,activityListItem);
            //获取活动发起人姓名
            String name = consumerMapper.selectOne(Wrappers.<Consumer>lambdaQuery()
                    .eq(Consumer::getAid,item.getActivityConsumerAid())
            ).getName();
            activityListItem.setName(name);
            //获取活动状态名称
            name = activityStateMapper.selectOne(Wrappers.<ActivityState>lambdaQuery()
                    .eq(ActivityState::getId,item.getStateId())
            ).getName();
            activityListItem.setState(name);
            activityLists.add(activityListItem);
        });

        activityListResult.setActivityList(activityLists);

        //最后一步，返回结果
        return ResponseWrapper.markSuccess(activityListResult);
    }

    /**
     * 2.新增/修改 活动信息
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    public ResponseWrapper saveActivity(SaveActivityParam param){
        Activity activity = new Activity();
        BeanUtil.copyProperties(param,activity);
        if (activity.getContent().length() >= 50){
            activity.setBriefContent(activity.getContent().substring(0,50));
        } else {
            activity.setBriefContent(activity.getContent());
        }
        if (activity.getAid() == -1){
            //新增
            activity.setAid(null);
            activity.setActivityConsumerAid(param.getTokenConsumerAid());
            activity.setStateId(10);
            activityMapper.insert(activity);
        } else {
            //修改
            activityMapper.updateById(activity);
        }

        //最后一步，返回结果
        return ResponseWrapper.markSuccess();
    }

    /**
     * 3.新增/修改 获取活动信息
     * @return
     */
    public ResponseWrapper activityInfo(ActivityInfoParam param){
        ActivityInfoResult activityInfoResult = new ActivityInfoResult();

        List<Activity> activities = activityMapper.selectList(
                Wrappers.<Activity>lambdaQuery()
                        .eq(Activity::getAid, param.getAid())
        );

        if (!activities.isEmpty()){
            Activity activity = activities.get(0);
            BeanUtil.copyProperties(activity,activityInfoResult);
            Consumer consumer = consumerMapper.selectOne(Wrappers.<Consumer>lambdaQuery().eq(Consumer::getAid, activity.getActivityConsumerAid()));
            activityInfoResult.setName(consumer.getName());
            ActivityState activityState = activityStateMapper.selectOne(
                    Wrappers.<ActivityState>lambdaQuery()
                            .eq(ActivityState::getId,activity.getStateId())
            );
            activityInfoResult.setStateName(activityState.getName());
        }

        //最后一步，返回结果
        return ResponseWrapper.markSuccess(activityInfoResult);
    }

    /**
     * 4.取消活动
     */
    @Transactional(rollbackFor = Exception.class)
    public ResponseWrapper cancelActivity(CancelActivityParam param) {
        Activity activity = new Activity();
        activity.setAid(param.getAid());
        if (param.getCancel()){
            //取消
            activity.setStateId(40);
        } else {
            //恢复正常
            activity.setStateId(10);
        }
        return ResponseWrapper.markSuccess();
    }
}